package com.doone.telmarket.domain.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.springframework.stereotype.Service;

import com.doone.telmarket.admin.action.ServiceTypeAction;
import com.doone.telmarket.domain.service.FileUploadService;
import com.opensymphony.xwork2.ActionContext;

/**
 * @package com.doone.telmarket.domain.service
 * @author 甘雨波
 * @description 文件操作服务实现类
 * @date 2009-9-18
 */
@Service("fileUploadService")
public class FileUploadServiceImpl implements FileUploadService {

	Logger log = Logger.getLogger(ServiceTypeAction.class);

	private FileOutputStream fos = null;
	private FileInputStream fis = null;

	/**
	 * @author 甘雨波
	 * @description 文件上传服务
	 * @date 2009-9-18
	 */
	public String uploadFiles(File[] Files, String[] fileNames) {

		try{
			ActionContext ac = ActionContext.getContext();
			ServletContext sc = (ServletContext) ac.get(StrutsStatics.SERVLET_CONTEXT);
			String path = sc.getRealPath("/");
			for (int i = 0; i < Files.length; i++) {
				//编辑增值业务时有可能不选择图片
				if(fileNames[i]==null){
					continue;
				}
				//写入文件
				fos = new FileOutputStream(path + fileNames[i]);
				fis = new FileInputStream(Files[i]);
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
			}
			if(fis!=null){
				fis.close();
			}
			if(fos!=null){
				fos.close();
			}
			return "E";
		}catch (Exception e) {
			log.info("上传文件失败:"+e.getMessage());
			return "F";
		}
	}

}
